package core.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	/**
	 * 语言转换器，在面板初始化完成之后调用 
	 * @author Ado
	 * @example
	 * <listing>
	 * //设入语言配置文件
	 * LanguageConvertor.instance.languageConfig(xml);
	 * 
	 * //处理面板
	 * LanguageConvertor.instance.operatePanel(panel);
	 * </listing>
	 */	
	public class LanguageConvertor
	{
		private static var _instance:LanguageConvertor;
		private var config:Dictionary;
		public function LanguageConvertor()
		{
			if(_instance)
			{
				throw new Error("Please use LanguageConvartor.instance instead...");
			}
			config = new Dictionary();
		}
		/**
		 * 设入语言配置文件 
		 * 
		 */		
		public function setLanguageConfig(xml:XML):void
		{
			var children:XMLList = xml.elements("item");
			var len:int = children.length;
			for each(var element:XML in children)
			{
				config[element.@key] = element.@value;
			}
		}
		/**
		 * 对面板进行本地化语言处理 <br/>
		 * 需要的游戏初始化的时候进行
		 * @param panel
		 * 
		 */		
		public function operatePanel(panel:DisplayObjectContainer):void
		{
			var len:int = panel.numChildren;
			while(len)
			{
				var child:DisplayObject = panel.getChildAt(len-1);
				if(child.hasOwnProperty("key"))
				{
					var key:String = child["key"];
					if(config.hasOwnProperty())
					{
						child["internationValue"] = config[key];	
					}else
					{
						trace("Key : " + key +" is not included in the language config...");
					}
				}else if(child is DisplayObjectContainer)
				{
					operatePanel(DisplayObjectContainer(child));
				}
				len--;
			}
		}

		public static function get instance():LanguageConvertor
		{
			if(null == _instance)
			{
				_instance = new LanguageConvertor();
			}
			return _instance;
		}

	}
}